//app.js
import touch from './pages/activitys/shopCart/touch.js'
import api from './utils/util.js'
App({
  globalData: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
    screenWidth: 0,
    screenHeight: 0,
    winWidth: 0,
    winHeight: 0,
    fullcontain: "", //除去导航全屏
    activeBeautician: '', // 已选中的美容师
    list: {}, //预约=>项目详情 选中的某一项（清空购物车）
    projectChange: '', // 用来项目详情页 判断用户是否点击了购物车栏的选技师
    host: "https://dev.himeili.com", //baseUrl地址
    ent: "10000", //企业编号
    storeId: "", // 门店id
    discount:{
      treatmentDiscount: 1,
      productDiscount: 1,
      projectDiscount: 1
    }
  },
  touch: new touch(),
  onLaunch: function () {
    let that = this
    // 隐藏系统tabbar
    wx.hideTabBar();
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res.windowWidth,res.windowHeight);
        // console.log(res.screenWidth,res.screenHeight);   //屏幕高度
        that.globalData.screenWidth = res.screenWidth;
        that.globalData.screenHeight = res.screenHeight;
        that.globalData.winWidth = res.windowWidth;
        that.globalData.winHeight = res.windowHeight;

        that.globalData.fullcontain = res.screenHeight - wx.getSystemInfoSync()['statusBarHeight'] - 45;
      }
    });
    if(that.checkLogin()){
      // 获取会员折扣权益
      api.post("/mini/my/account/discount",{
          token: wx.getStorageSync('token')
      }).then(res => {
        that.globalData.discount.treatmentDiscount = res.result.treatmentDiscount,
        that.globalData.discount.productDiscount = res.result.productDiscount,
        that.globalData.discount.projectDiscount = res.result.projectDiscount
        console.log("用户折扣权益：",that.globalData.discount)
      })
    }
 },
  onShow: function () {
    wx.hideTabBar(); //隐藏系统tabbar
  },
  //检查登录状态(token时间校验)
  checkLogin() {
    let token = wx.getStorageSync('token'),
        createTime = wx.getStorageSync('createTime');
      console.log("->检查登录状态");
    if (token && createTime) {
      let time = new Date();
          time = time.getTime();
      // 设置3天更新登录状态
      if(time - createTime > 3 * 24 * 3600 * 1000){
        console.log("->token已过期")
        return false
      }else{
        console.log('->token有效存在')
        return true
      }
    } else {
      // 重新登录
      return false
    }
  },
  // 获取用户信息
  getuserinfo() {
    wx.getUserInfo({
      success: (res) => {
        console.log(res.userInfo)
        var json = res.userInfo;
        // this.setData({
        //   nickname:json.nickName,
        //   pri: json.province,
        //   city:json.city,
        //   img:json.avatarUrl
        // })
      }
    })
  },
  // 路径导航
  goto(url, mes) {
    wx.navigateTo({
      url: url + '?mesObj=' + JSON.stringify(mes),
    })
  },
  // 这里这么写，是要在其他界面监听，而不是在app.js中监听，而且这个监听方法，需要一个回调方法。 name监听名字
  watch:function(method, name){
    var obj = this.globalData;
    Object.defineProperty(obj,name, {
      configurable: true,
      enumerable: true,
      set: function (value) {
        method(value); // 回调
      }
    })
  },
})
